//
//  LeftSideMenuTableViewController.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/1.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "LeftSideMenuTableViewController.h"
#import "FreindCell.h"
#import "MooreDollViewController.h"
#import "FriendViewController.h"
#import "MessageViewController.h"
#import "DrawerUserCell.h"
#import "DrawerCell.h"
#import "WebViewController.h"
#import "SearchFriendViewController.h"
#import "SettingViewController.h"
#import "ConnectStateDelegate.h"
#import "UserInfoTableViewController.h"
#import "UITableView+Separator.h"
@interface LeftSideMenuTableViewController ()

@end

@implementation LeftSideMenuTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    //左側菜單的選項標題
    self.Array_title = @[@"",@"魔豆精靈",@"市集",@"尋找好友",@"設定",@"Mooregoll Facebook",@"分享到Facebook",@"登出",@"登入"];
    
    self.userInfo = [UserInfoModel sharedInstance];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeUserInfo:) name:@"changeUserInfo" object:nil];
}
-(void)changeUserInfo:(NSNotification *)notification
{
    self.userInfo = [notification object];
    [self.LeftTableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.Array_title.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //設定第一個cell 高度 （使用者圖片資料）
    if (indexPath.row==0) {
        //依照螢幕寬度來設定Cell 高度
        return CGRectGetWidth([UIScreen mainScreen].bounds)*0.625;
    }
    
    //依照螢幕寬度來設定Cell 高度
    return CGRectGetWidth([UIScreen mainScreen].bounds)*0.1375;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row==0) {
        //使用者設定
        static NSString *headerCellIdentifier = @"DrawerUserCell";
        
        DrawerUserCell *drawerUserCell = (DrawerUserCell*)[tableView dequeueReusableCellWithIdentifier:headerCellIdentifier];
        
        if (drawerUserCell == nil) {
            
            [tableView registerNib:[UINib nibWithNibName:@"DrawerUserCell" bundle:nil] forCellReuseIdentifier:headerCellIdentifier];
            
            drawerUserCell = [tableView dequeueReusableCellWithIdentifier:headerCellIdentifier];
        }
        if (self.userInfo)
        {
            drawerUserCell.IM_UserPhoto.image = [UIImage imageWithContentsOfFile:self.userInfo.ImagePath];
            drawerUserCell.LB_Name.text = self.userInfo.NICKNAME ;
            drawerUserCell.LB_Email.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"account"]?[[NSUserDefaults standardUserDefaults]objectForKey:@"account"]:@"無名氏";
        }else
        {
            drawerUserCell.IM_UserPhoto.image = [UIImage imageNamed:@"userImage"];
        }
        
        
        return drawerUserCell;
        
    }else
    {
        static NSString *cellIdentifier = @"DrawerCell";
        
        DrawerCell *cell = (DrawerCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        
        if (cell == nil) {
            
            [tableView registerNib:[UINib nibWithNibName:@"DrawerCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
            
            cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        }
        
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"slider_icon0%i",indexPath.row]];
        
        cell.textLabel.text = self.Array_title[indexPath.row];
        
        return cell;
        
        
    }
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //取得TabBarController
    UITabBarController *tabbarController =self.menuContainerViewController.centerViewController;
    
    //取得navigationController
    UINavigationController *navigationController = (UINavigationController *)tabbarController.selectedViewController;
    
    //使用者資料
    UserInfoTableViewController *userInfoVC;
    //Web畫面
    WebViewController* webVC;
    //尋找朋友
    SearchFriendViewController *searchFriendVC;
    //環境設定
    SettingViewController* settingVC;
    
    switch (indexPath.row) {
        case 0:
            
            userInfoVC = [self.storyboard instantiateViewControllerWithIdentifier:@"UserInfoTableViewController"];
            [navigationController pushViewController:userInfoVC animated:YES];
            //            tabbarController.selectedIndex = 0;
            break;
        case 1:
            //            tabbarController.selectedIndex = 1;
            
            break;
        case 2:
            //            tabbarController.selectedIndex = 2;
            webVC = [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
            webVC.webURLString = @"https://www.google.com.tw";
            
            [navigationController pushViewController:webVC animated:YES];
            
            break;
            
        case 3:
            
            searchFriendVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SearchFriendViewController"];
            
            [navigationController pushViewController:searchFriendVC animated:YES];
            
            break;
        case 4:
            settingVC = [self.storyboard instantiateViewControllerWithIdentifier:@"SettingViewController"];
            [navigationController pushViewController:settingVC animated:YES];
            
            break;
        case 5:
            
            break;
        case 6:
            
            break;
        case 7:
            [[XMPPSharedInstance sharedInstance]logoutXMPP];
            tabbarController.selectedIndex = 0;

                if ([[navigationController.viewControllers lastObject]isKindOfClass:[FriendViewController class]]) {
                    
                    //如果沒有儲存帳號 就轉到登入畫面
                    LoginOrRegistViewController *loginVC = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginOrRegistViewController"];
                    [navigationController pushViewController:loginVC animated:NO];
                    
                }else
                {
                    [navigationController popToRootViewControllerAnimated:NO];
                }
                
            
            
            
            break;
        case 8:
            [[XMPPSharedInstance sharedInstance]loginXMPP];
            break;
        default:
            break;
    }
    //點選完之後關閉側邊欄
    [self.menuContainerViewController setMenuState:MFSideMenuStateClosed];
    
    
}


/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
